package com.daji.base_data_structure.LinkedList;

public class LinkedListApi {

    /*
    * 初始化一个链表，入参是数组
    * */
    public static ListNode initLinkedList(int[] arr){
        if (arr.length == 0){   //边界检查
            return null;
        }
        //构建头结点
        ListNode head = new ListNode(arr[0],null);
        ListNode workNode = head;
        for (int i = 1; i < arr.length; i++) {
            ListNode node = new ListNode(arr[i],null);  //追加结点
            workNode.next = node;
            workNode = workNode.next;
        }
        return head;
    }

    /*
    * 遍历打印一个链表
    * */
    public static void printLinkedList(ListNode head){
        StringBuilder stringBuilder = new StringBuilder();
        while(head != null){
            stringBuilder.append(head.val);
            stringBuilder.append(",");
            head = head.next;
        }
        System.out.println(stringBuilder);
    }
}
